package org.vuzya.motivateme.activities;

import static org.vuzya.motivateme.helpers.ConstantHelper.EXTRA_BUSINESS_ID;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.vuzya.motivateme.R;
import org.vuzya.motivateme.core.DragableSpace;
import org.vuzya.motivateme.db.DBHelper;
import org.vuzya.motivateme.models.Motivations;
import org.vuzya.motivateme.models.QuestionAnswers;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class TableActivity extends Activity {
	private static final String TAG = "COMPARE_TABLE_ACTIVITY";
	
	private static final String[] from = new String[] { QuestionAnswers.ANSWER, Motivations.MOTIVATION_DATE };
	private static final int[] to = new int[] { android.R.id.text1, android.R.id.text2 };
	
	private long businessId;
	private Cursor cursor;
	private DragableSpace dragableSpace;
	
	private String[] questions;
	private LayoutInflater li;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		businessId = getIntent().getLongExtra(EXTRA_BUSINESS_ID, -1);
		
		questions = getResources().getStringArray(R.array.question_titles);
		li = getLayoutInflater();
				
		DBHelper dbh = new DBHelper(this);
		try {
			cursor = dbh.getQuestionAnswers(businessId);
			dragableSpace = generateDragableSpace();
		} catch (Exception e) {
			Log.d(TAG, "DB error");
		} finally {
			dbh.close();
		}

		setContentView(dragableSpace);
	}

	@Override
	protected void onDestroy() {
		cursor.close();
		super.onDestroy();
	}

	private DragableSpace generateDragableSpace() {
		DragableSpace ds = new DragableSpace(this);
		ds.setBackgroundResource(R.color.background);
		
		if(cursor == null){
			return ds;
		}
		
		int questionNumber = -1;
		List<Map<String, String>> data = null; 

		int newQuestionNumber;
		Map<String, String> elem;
		
		while(cursor.moveToNext()){
			newQuestionNumber = cursor.getInt(cursor.getColumnIndex(QuestionAnswers.QUESTION_NUMBER));
			if(questionNumber != newQuestionNumber){
				if(data != null){
					ds.addView(fillItem(questionNumber, data));
				}
				
				questionNumber = newQuestionNumber;
				data = new ArrayList<Map<String,String>>();
			}
			
			elem = new HashMap<String, String>();
			elem.put(QuestionAnswers.ANSWER, cursor.getString(cursor.getColumnIndex(QuestionAnswers.ANSWER)));
			elem.put(Motivations.MOTIVATION_DATE, cursor.getString(cursor.getColumnIndex(Motivations.MOTIVATION_DATE)));
			data.add(elem);
		}
		
		if(data != null){
			ds.addView(fillItem(questionNumber, data));
		}
		
		return ds;
	}

	private View fillItem(int questionNumber, List<Map<String, String>> data) {
		View item = li.inflate(R.layout.table_column_item, null);
		
		((TextView)item.findViewById(R.id.table_column_item_question)).setText(questions[questionNumber]);
		ListView lv = (ListView)item.findViewById(R.id.table_column_item_list);
		if(questionNumber == 0){
			((TextView)item.findViewById(R.id.table_column_item_back)).setVisibility(View.GONE);
		}
		if(questionNumber == questions.length - 1){
			((TextView)item.findViewById(R.id.table_column_item_next)).setVisibility(View.GONE);
		}
		
		SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, from, to);
		lv.setAdapter(adapter);
		
		return item;
	}
}
